/*
 *	Copyright © 2013 Changsha Shishuo Network Technology Co., Ltd. All rights reserved.
 *	长沙市师说网络科技有限公司 版权所有
 *	http://www.shishuo.com
 */

package com.hlingsoft.kkh.action;

import com.hlingsoft.kkh.constant.ApplicationConstant;
import com.hlingsoft.kkh.dao.AdminApplicationDao;
import com.hlingsoft.kkh.dao.MerchantDao;
import com.hlingsoft.kkh.dao.MessageDao;
import com.hlingsoft.kkh.entity.AppUser;
import com.hlingsoft.kkh.entity.AppUserMessage;
import com.hlingsoft.kkh.entity.ApplicationHistory;
import com.hlingsoft.kkh.entity.MerchantInfo;
import com.hlingsoft.kkh.service.AdminPushService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 首页
 *
 * @author Herbert
 */
@Controller
@RequestMapping("/merchant")
public class MerchantAction extends BaseAction {

    @Autowired
    protected MerchantDao merchantDao;

    @Autowired
    private AdminApplicationDao applicationDao;



    @Autowired
    private MessageDao messageDao;
    private static final int rows = 100;

    /**
     * 登陆验证
     * @param phone
     * @param request
     * @return
     */
    @RequestMapping(value = "/verifyCode.json", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> verifyCode(
            @RequestParam(value = "phone") String phone,
            HttpServletRequest request) {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        try {
            resultMap.put("verifyCode", "123456");
            resultMap.put("result", true);

        } catch (Exception e) {
            e.printStackTrace();
            resultMap.put("result", false);
        }

        return resultMap;

    }

    /**
     * 登陆
     * @param phone
     * @param udid
     * @param request
     * @return
     */
    @RequestMapping(value = "/login.json", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> login(
            @RequestParam(value = "phone") String phone,
            @RequestParam(value = "udid") final String udid,
            HttpServletRequest request) {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        try {
            MerchantInfo user = merchantDao.getMerchantByPhone(phone);

            if (user instanceof MerchantInfo) {
                user.setUdId(udid);
                merchantDao.updateMerchant(user);
                resultMap.put("user", user);
                resultMap.put("result", true);
            } else {
                resultMap.put("result", false);
            }


        } catch (Exception e) {
            e.printStackTrace();
            resultMap.put("result", false);
        }

        return resultMap;

    }

    /**
     * 面试
     * @param merchantApplicationId
     * @param request
     * @return
     */
    @RequestMapping(value = "/agreeApplication.json", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> agreeApplication(
            @RequestParam(value = "merchantApplicationId") long merchantApplicationId,
            HttpServletRequest request) {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        try {
            //更新用户的申请状态
            ApplicationHistory merchantApplication = applicationDao.getMerchantApplicationByApplicationId(merchantApplicationId); //商户传递applicationId查询出merchantApplication(对应表merchant_application_history)
            ApplicationHistory appApplication = applicationDao.getUserApplicationByJobIdAndResumeId(merchantApplication.getJobId(), merchantApplication.getResumeId());
            //通过merchantApplication中的jobId,和resumeId,查询出用户applicationHistory(对应表application_history)
            final MerchantInfo merchantInfo = merchantDao.getMerchantById(merchantApplication.getMerchantId());
            final  AppUser appUser=userDao.getUserById(appApplication.getAppUserId());
            appApplication.setStatus(ApplicationConstant.Status.AGREE);
            appApplication.setUpdateTime(new Date());
            applicationDao.updateUserApplication(appApplication);
            merchantApplication.setStatus(ApplicationConstant.Status.NOTICE);
            merchantApplication.setUpdateTime(new Date());
            applicationDao.updateMerchantApplication(merchantApplication) ;
            //向用户推送
            //System.out.println(appUser.getUdid());
            String alert = merchantInfo.getMerchantName() + "同意了你的面试申请";
            adminPushService.pushToClient(alert, new ArrayList<String>() {
                {
//                    add( appUser.getUdid());
                }
            }, AdminPushService.JOB,false,true);
            AppUserMessage messageVo = new AppUserMessage();
            messageVo.setCreateTime(new Date());
            messageVo.setContent(alert);
            messageVo.setAppUserId(appUser.getId());
//            messageVo.setStatus(ApplicationConstant.Status.DONE);
            messageVo.setExtra(Integer.valueOf(AdminPushService.JOB));
            messageVo.setTitle("申请记录");
            messageDao.addUserMessage(messageVo)    ;
            resultMap.put("result", true);
//            applicationDao.addMerchantMessage(applicationHistory);
        } catch (Exception e) {
            e.printStackTrace();
            resultMap.put("result", false);
        }

        return resultMap;

    }

    @RequestMapping(value = "/disAgreeApplication.json", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> disAgreeApplication(
            @RequestParam(value = "merchantApplicationId") long merchantApplicationId,
            HttpServletRequest request) {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        try {
            //更新用户的申请状态
            ApplicationHistory merchantApplication = applicationDao.getMerchantApplicationByApplicationId(merchantApplicationId); //商户传递applicationId查询出merchantApplication(对应表merchant_application_history)
            ApplicationHistory appApplication = applicationDao.getUserApplicationByJobIdAndResumeId(merchantApplication.getJobId(), merchantApplication.getResumeId());
            //通过merchantApplication中的jobId,和resumeId,查询出用户applicationHistory(对应表application_history)
            final MerchantInfo merchantInfo = merchantDao.getMerchantById(merchantApplication.getMerchantId());
            appApplication.setStatus(ApplicationConstant.Status.REJECT);
            appApplication.setUpdateTime(new Date());
            applicationDao.updateUserApplication(appApplication);
            merchantApplication.setStatus(ApplicationConstant.Status.DONE);
            merchantApplication.setUpdateTime(new Date());
            applicationDao.updateMerchantApplication(merchantApplication);


//            applicationDao.deleteMerchantApplication(merchantApplication.getId())  ;
//            applicationDao.deleteUserApplication(appApplication.getId());


            //向用户推送


            String alert = merchantInfo.getMerchantName() + "拒绝了你的面试申请";
            adminPushService.pushToClient(alert, new ArrayList<String>() {
                {
                    add(merchantInfo.getUdId());
                }
            }, AdminPushService.JOB,true,true);
            AppUserMessage messageVo = new AppUserMessage();
            messageVo.setCreateTime(new Date());
            messageVo.setContent(alert);
            messageVo.setExtra(Integer.valueOf(AdminPushService.JOB));
            messageVo.setTitle("申请记录");
            messageDao.addUserMessage(messageVo)    ;
            resultMap.put("result", true);
//            applicationDao.addMerchantMessage(applicationHistory);
        } catch (Exception e) {
            e.printStackTrace();
            resultMap.put("result", false);
        }

        return resultMap;

    }


    @RequestMapping(value = "/detail.json", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> list(
            @RequestParam(value = "merchantId") long merchantId,

            HttpServletRequest request) {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        try {

            resultMap.put("merchant", merchantDao.getMerchantById(merchantId));
            resultMap.put("result", true);

        } catch (Exception e) {
            e.printStackTrace();
            resultMap.put("result", false);
        }

        return resultMap;

    }


}
